<?php

/**
 * @package Nina
 * @author Andrew Rose, Chris Corbyn, John Main <john@thedaddy.org>
 */
class Nina
{
	/**
	 * Version number of the library.
	 */
	const VERSION = 0.1;
	
	/**
	 * Get the version number of this library.
	 * @return float
	 */
	public static function getVersion() {
		return self::VERSION;
	}
	/**
	 * Include a class by its name.
	 * @param string $class_name Class name
	 */
	public static function loadClass($class_name) {
		require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
	}
	/**
	 * Look for a class and see if it exists.
	 * @param string $class_name Class name
	 * @return string Path of class or false if unable to find
	 */
	public static function classExists($class_name) {
		$file = dirname(__FILE__) . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
		if (file_exists($file)) {
			return $file;
		}
		else
			return false;
	}

	public static function setupAutoloader() {
		$root = realpath(dirname(__FILE__));
		require_once $root.'/Nina/Autoloader.php';
		require_once $root.'/Nina/Autoloader/Nina.php';
		$loader = new Nina_Autoloader_Nina($root.'/');
		$loader->register();
	}

	/**
	* Handles any uncaught exceptions
	* @param Exception $e The exception
	*/
	public static function exceptionHandler($e) {
		$logger = Nina_Logger::getInstance();
		$reg = Nina_Registry::getInstance();

		if ($reg->get('logging.verbose')) {
			echo nl2br($e->getMessage());
			echo '<br />';
			echo nl2br($e->getTraceAsString());
			echo '<br />';
		}

		error_log($e->getMessage());
		$logger->error($e->getMessage());
		$traces = explode("\n", $e->getTraceAsString());

		foreach($traces as $trace) {
			error_log($trace);
			$logger->err($trace);
		}
	}
}
